using System;
using System.Diagnostics;
using RpgXna.Model.Menu;
using RpgXna.Model.XnaClassesWrappers;

namespace RpgXna.Model.Interfaces.Inventory.NavigationBehaviors
{
    internal class InventoryNavigationBehavior : KeysInputBehavior
    {
        public InventoryNavigationBehavior(Func<bool> predicate)
        {
            Predicate = predicate;
        }

        public Slot Execute(Slot[,] slots, IKeyboardStateWrapper kbs, Slot selectedSlot)
        {
            if (!Predicate() /*|| !kbs.IsAnyKeyDown(InputBindings.Bindings.MoveBindings)*/) return selectedSlot;

            CurKbs = kbs;
            int y = selectedSlot.Y;
            int x = selectedSlot.X;
            if (KeyPressed(InputBindings.Bindings.Up))
            {
                slots[x, y].IsSelected = false;

                if (y == 0)
                {
                    y = slots.GetLength(1) - 1;
                }
                else
                {
                    y = y - 1;
                }
                slots[x, y].IsSelected = true;

                Debug.WriteLine("UP x=" + x + ";y=" + y);
            }
            if (KeyPressed(InputBindings.Bindings.Left))
            {
                slots[x, y].IsSelected = false;

                if (x == 0)
                {
                    x = slots.GetLength(0) - 1;
                }
                else
                {
                    x = x - 1;
                }
                slots[x, y].IsSelected = true;

                Debug.WriteLine("LEFT x=" + x + ";y=" + y);
            }
            if (KeyPressed(InputBindings.Bindings.Down))
            {
                slots[x, y].IsSelected = false;

                if (y == slots.GetLength(1) - 1)
                {
                    y = 0;
                }
                else
                {
                    y = y + 1;
                }
                slots[x, y].IsSelected = true;

                Debug.WriteLine("DOWN x=" + x + ";y=" + y);
            }

            if (KeyPressed(InputBindings.Bindings.Right))
            {
                slots[x, y].IsSelected = false;

                if (x == slots.GetLength(0) - 1)
                {
                    x = 0;
                }
                else
                {
                    x = x + 1;
                }
                slots[x, y].IsSelected = true;

                Debug.WriteLine("RIGHT x=" + x + ";y=" + y);
            }

            Prevkbs = kbs;

            return slots[x, y];
        }


    }
}